<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<%@ page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%boolean editAuthFlag = UserFunctionValidate.resFunctionValidate(Long.valueOf(String.valueOf(request.getAttribute("resId"))), FunctionConstants.EDIT_RES);%>
<html>
  <head>
    <title>自定义属性</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<%@ include file="../include/form.jsp"%>
	<%@ include file="../include/table.jsp"%>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#attributes").dataTable({
				"bProcessing": true, 
				"bServerSide": true,
				"sServerMethod": 'POST',
				"sAjaxSource":"attribute!list.action?resId=${resId}",
				"bPaginate":false,
				"bSort": true,
				"bFilter":false,
				"sPaginationType": 'full_numbers',
				"aoColumns":[
					<% if(editAuthFlag){%>
						{"mDataProp":"operate","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
							$(nTd).html("<div style='width:50px; padding-left:22px;display:${hasEditResPrivilege?'block':'none'};'  ><a href='#"+oData.id+"' onclick='openUpdateWindow("+oData.id+")'><img src='../style/default/bsm/images/edit.gif' title='修改'></a>&nbsp;&nbsp;<a href='#' onclick='del("+oData.id+")'><img src='../style/default/bsm/images/delete.gif' title='删除' /></a></div>");
						}},
					<%}%>
					{"mDataProp":"name","bSortable": false},
					{"mDataProp":"dataTypeName","bSortable": false},
					{"mDataProp":"desc","bSortable": false},
					{"mDataProp":"id","bVisible":false},
					{"mDataProp":"dataTypeId","bVisible":false}
				],
				"oLanguage":{
					"sZeroRecords": "没有找到记录",
					"sInfo":"",
					"sInfoEmpty": ""
				}
			});
		});
		
		
		function openWindow(){
			var url ='iframe:resource/attribute!input.action?resId=${resId}';
		    var opt =  {title:'新增属性',id:'inputAttribute',width:555,height:270,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
		    getTopWin(window).openjBox(url,opt,'inputAttributeWindow',window);
		}
		
		
		//增加属性
		addAttribute = function (attribute){
			 $.ajax({
					type:'post',
					url:'attribute!save.action?resId=${resId}',
					data:attribute,
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						var oTable =$('#attributes').dataTable();
						getTopWin(window).getMessageBox().tip('添加属性成功。', 'success'); 
						oTable.fnClearTable(); 
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
					}
			});
		}
		
		function del(id){
			 if(isDel(id)==true){
			 	getTopWin(window).getMessageBox().alert('属性已被删除',"");
			 	var table =$("#attributes").dataTable();
			 	table.fnClearTable();
			 	return;
			 }
			 var submit = function(v, h, f){
				  	if (v == 'ok'){
				  		getTopWin(window).getMessageBox().tip("正在删除资源属性...", 'loading');
						$.ajax({
								type:'post',
								url:'attribute!delete.action',
								data:{id:id,resId:'${resId}'},
								global:false,
								async:false,
								cache:false,
								dataType:'json',
								success:function(data,textStatus){
									var oTable =$('#attributes').dataTable();
									window.setTimeout(function () {
									        if(data.flag=="false"){
									        	getTopWin(window).getMessageBox().tip(data.info, 'info'); 
									        }else{
									        	getTopWin(window).getMessageBox().tip('删除属性成功', 'success'); 
												oTable.fnClearTable(); 
									        }
									},1500);
								},
								error:function(){
									getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
								}
						});
				}
			};
			getTopWin(window).getMessageBox().confirm('确定删除?','提示',submit);
	   }
		
		function openUpdateWindow(id){
			if(isDel(id)==false){
				var url ='iframe:resource/attribute!initUpdate.action?resId=${resId}&id='+id;
		    	var opt =  {title:'修改属性',id:'updateAttribute',width:555,height:270,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
		   	 	getTopWin(window).openjBox(url,opt,'updateAttributeWindow',window);
			}else{
				getTopWin(window).getMessageBox().alert('属性已被删除',"");
				var table =$("#attributes").dataTable();
			 	table.fnClearTable();
			}
		}
		
		update = function(attribute){
			if(isDel(attribute.id)){
				getTopWin(window).getMessageBox().tip('属性已被删除',"");
				var table =$("#attributes").dataTable();
			 	table.fnClearTable();
			 	return;
			}
		
			$.ajax({
					type:'post',
					url:'attribute!save.action?resId=${resId}',
					data:attribute,
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						var oTable =$('#attributes').dataTable();
						getTopWin(window).getMessageBox().tip('修改属性成功。', 'success'); 
						oTable.fnClearTable(); 
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
					}
			});
		}
		
		//验证是否删除
		function isDel(id){
			var isFlag = false;
			$.ajax({
					type:'post',
					url:'attribute!isDelete.action',
					data:{id:id},
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						isFlag = data;
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('检验属性是否删除出错!','错误');
					}
			});
			return isFlag;
		}
	</script>
  </head>
  
  <body >
		<div class ="toolbar" id="toolbar" style="display:${hasEditResPrivilege?'block':'none'};">
			<a href="#" onclick="openWindow()" class="page_add"  >新增属性</a>
		</div>
		<table width="100%"  id="attributes" cellpadding="0" cellspacing="0" border="0" class="display"  >
			<thead>
    	 		<tr>
    	 		<% if(editAuthFlag){%>
    	 			<th width="90px">操作</th>
    	 		<%}%>
    	 			<th width="120px">属性名称</th>
    	 			<th width="120px">数据类型及单位</th>
    	 			<th>描述</th>
    	 		</tr>
    	 	</thead>
		</table>
  </body>
</html>
